package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;

import com.sky.result.PageResult;

import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分页查询分类管理列表")
    public Result<PageResult> listByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.listByPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.update(category);
        return Result.success("修改成功");
    }

    @ApiOperation("根据categoryId查询分类")
    @GetMapping("list")
    public Result listById(Integer type){
        List<Category> categoryList = categoryService.ListByType(type);
        return Result.success(categoryList);
    }

    @ApiOperation("删除分类")
    @DeleteMapping
    public Result deleteById(Integer id) {
        categoryService.delete(id);
        return Result.success("删除成功");
    }

    @ApiOperation("新增分类")
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO){
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.insert(category);
        return Result.success("新增成功");
    }
}
